import { Component } from '@angular/core';
import { NavParams, NavController } from 'ionic-angular';

import { StorageService } from '../../providers/StorageService';
import { HistoryService } from '../../providers/HistoryService';

import { HistoryDetailPage } from '../historyDetail/historyDetail';

@Component({
  selector: 'page-history',
  templateUrl: 'history.html',
  providers: [HistoryService]
})
export class HistoryPage {
  user_info: any;
  user_id: number;
  room: any;
  room_id: number;
  room_name: string;
  check_in_id: number;
  histories: any;
  historyPage = 1;

  constructor(
    navParams: NavParams,
    private navCtrl: NavController,
    private storageService: StorageService,
    private historyService: HistoryService
  ) {
    this.user_info = this.storageService.read('UserInfo');
    this.user_id = this.user_info.user_id;

    this.room = this.storageService.read('RoomInfo');
    this.room_id = this.room.room_id;
    this.room_name = this.room.room_name;

    this.check_in_id = navParams.get('check_in_id');

    this.historyService.getList({'user_id': this.user_id, 'room_id': this.room_id, 'page': this.historyPage}).then(data => {
      this.histories = data.history;
      this.historyPage++;
    });
  }

  registerViewHistoryDetail(history) {
    this.navCtrl.push(HistoryDetailPage, { check_in_id: history.check_in_id });
  }

  doInfinite(infiniteScroll) {
    setTimeout(() => {
      this.historyService.getList({'user_id': this.user_id, 'room_id': this.room_id, 'page': this.historyPage}).then(data => {
        if (data.history.length > 0) {
          this.histories = this.histories.concat(data.history);
          this.historyPage++;
        }
      });
      infiniteScroll.complete();
    }, 500);
  }
}
